package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Constant;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.model.UserInfoData;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.view.MainActivity;

/**
 * Created by xlm on 2017/10/27 0027
 */

public class LauncherActivity extends BaseActivity {

    @Override
    protected void onSetContentView() {

    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        checkPermission();
    }

    /**
     * 检查权限，因为android6.0有些权限需要用户确认授权
     */
    private static final int PERMISSON_SOTRAGE = 0;
    private static final int PERMISSON_AUDIO = 1;

    private void checkPermission() {
        if (checkStoragePermission(PERMISSON_SOTRAGE)) {
            if (checkRecordAudioPermission(PERMISSON_AUDIO)) {
                permissionOk();
            }
        }
    }

    @Override
    public void onRecordAudioPermission(int result_code, boolean result) {
        if (result) {
            checkPermission();
        } else {
            showToast("拒绝此权限，您将无法正常使用软件");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            }, 5000);
        }
    }

    @Override
    public void onStoragePermission(int result_code, boolean result) {
        if (result) {
            checkPermission();
        } else {
            showToast("拒绝此权限，您将无法正常使用软件");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            }, 5000);
        }
    }

    /**
     * 所有权限都已经获取
     */
    private void permissionOk() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (CommonUtil.isEmpty(String.valueOf(PreferenceUtil.getParam(Preferences.USERNAME, "")))) {
                    startActivity(LoginModeActivity.class);
                } else {
                    getLoginData(String.valueOf(PreferenceUtil.getParam(Preferences.USERNAME, "")),
                            String.valueOf(PreferenceUtil.getParam(Preferences.PASSWORD, "")),
                            String.valueOf(PreferenceUtil.getParam(Preferences.LOGINID, "")),
                            String.valueOf(PreferenceUtil.getParam(Preferences.LOGINTYPE, "")));
                }
            }
        }, 1200);
    }

    @Override
    protected void onSetListener() {

    }

    /**
     * 获取本地信息 自动登录
     *
     * @param mobile
     * @param password
     * @param loginid
     * @param logintype
     */
    private void getLoginData(String mobile, String password, String loginid, String logintype) {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", "User.User.login");
        param.add("username", mobile);
        param.add("password", password);
        if (Constant.LOGIN_SHOP.equals(logintype)
                || Constant.LOGIN_TERMINAL.equals(logintype)) {
            param.add("login_id", loginid);
        }
        param.add("login_type", logintype);
        param.add("sign", param.getMd5String());
        showLoading("登陆中");
        HttpUtils.getInstance().url(ApiName.API).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissLoading();
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissLoading();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        UserInfoData userInfoData = GsonUtil.GsonToBean(string, UserInfoData.class);
                        Intent intent = new Intent(getThis(), MainActivity.class);
                        intent.putExtra(Constant.UID, String.valueOf(userInfoData.getData().getId()));
                        intent.putExtra(Constant.LOGIN_ID, String.valueOf(userInfoData.getData().getLogin_id()));
                        intent.putExtra(Constant.LOGIN_TYPE, String.valueOf(userInfoData.getData().getLogin_type()));
                        startActivity(intent);
                        finish();
                    } else {
                        showToast(result.getInfo());
                        startActivity(LoginModeActivity.class);//自动登录失败
                    }
                } else {
                    showToast(string);
                    startActivity(LoginModeActivity.class);//自动登录失败
                }
            }
        });
    }
}
